package com.system.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.business.domain.DailyTrain;
import com.system.business.domain.DailyTrainTicket;
import com.system.business.domain.TrainCarriage;
import com.system.business.domain.TrainSeat;
import com.system.business.enums.SeatColEnum;
import com.system.business.req.TrainSeatQueryReq;
import com.system.business.req.TrainSeatSaveReq;
import com.system.business.resp.DailyTrainQueryResp;
import com.system.business.resp.TrainSeatQueryResp;
import com.system.business.service.TrainCarriageService;
import com.system.business.service.TrainSeatService;
import com.system.business.mapper.TrainSeatMapper;
import com.system.common.resp.PageResp;
import com.system.common.utils.SnowUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
* @author Admin
* @description 针对表【train_seat(座位)】的数据库操作Service实现
* @createDate 2024-03-21 15:02:16
*/
@Service
public class TrainSeatServiceImpl extends ServiceImpl<TrainSeatMapper, TrainSeat>
    implements TrainSeatService{
    private static final Logger LOG = LoggerFactory.getLogger(TrainSeatService.class);

    @Autowired
    private TrainSeatMapper trainSeatMapper;
    @Autowired
    private TrainCarriageService trainCarriageService;

    @Override
    @Transactional
    public void genTrainSeat(String trainCode) {
        //清空当前车次下的所有座位记录
        QueryWrapper<TrainSeat> trainSeatQueryWrapper = new QueryWrapper<>();
        trainSeatQueryWrapper.eq("train_code",trainCode);
        trainSeatMapper.delete(trainSeatQueryWrapper);
        DateTime now = DateTime.now();
        //查找当前车次下的所有车厢
        QueryWrapper<TrainCarriage> trainCarriageQueryWrapper = new QueryWrapper<>();
        trainCarriageQueryWrapper.eq("train_code",trainCode);
        List<TrainCarriage> trainCarriages = trainCarriageService.list(trainCarriageQueryWrapper);

        //循环生成每个车厢的座位
        for (TrainCarriage trainCarriage : trainCarriages) {
            // 拿到车厢数据：行数、座位类型(得到列数)
            Integer rowCount = trainCarriage.getRowCount();
            String seatType = trainCarriage.getSeatType();
            int seatIndex = 1;

            // 根据车厢的座位类型，筛选出所有的列，比如车箱类型是一等座，则筛选出columnList={ACDF}
            List<SeatColEnum> colEnumList = SeatColEnum.getColsByType(seatType);
            LOG.info("根据车厢的座位类型，筛选出所有的列：{}", colEnumList);

            // 循环行数
            for (int row = 1; row <= rowCount; row++) {
                // 循环列数
                for (SeatColEnum seatColEnum : colEnumList) {
                    // 构造座位数据并保存数据库
                    TrainSeat trainSeat = new TrainSeat();
                    trainSeat.setId(SnowUtil.getSnowflakeNextId());
                    trainSeat.setTrainCode(trainCode);
                    trainSeat.setCarriageIndex(trainCarriage.getIndexCode());
                    trainSeat.setRowNum(StrUtil.fillBefore(String.valueOf(row), '0', 2));
                    trainSeat.setCol(seatColEnum.getCode());
                    trainSeat.setSeatType(seatType);
                    trainSeat.setCarriageSeatIndex(seatIndex++);
                    trainSeat.setCreateTime(now);
                    trainSeat.setUpdateTime(now);
                    trainSeatMapper.insert(trainSeat);
                }
            }
        }
    }

    @Override
    public void save(TrainSeatSaveReq req) {
        DateTime now = DateTime.now();

        TrainSeat trainSeat = BeanUtil.copyProperties(req, TrainSeat.class);
        if (ObjectUtil.isNull(trainSeat.getId())) {
            trainSeat.setId(SnowUtil.getSnowflakeNextId());
            trainSeat.setCreateTime(now);
            trainSeat.setUpdateTime(now);
            trainSeatMapper.insert(trainSeat);
        } else {
            trainSeat.setUpdateTime(now);
            trainSeatMapper.updateById(trainSeat);
        }
    }

    @Override
    public PageResp<TrainSeatQueryResp> queryList(TrainSeatQueryReq req) {
        Page<TrainSeat> pageCondition = new Page<>(req.getPage(), req.getSize());
        QueryWrapper<TrainSeat> passengerQueryWrapper = new QueryWrapper<>();
        Page<TrainSeat> pageData = trainSeatMapper.selectPage(pageCondition,passengerQueryWrapper);
        List<TrainSeatQueryResp> list = BeanUtil.copyToList(pageData.getRecords(), TrainSeatQueryResp.class);

        PageResp<TrainSeatQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageData.getTotal());
        pageResp.setList(list);
        return pageResp;
    }
}




